package com.example.tao.mycar.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;

import com.example.tao.mycar.MyApplication;

import java.io.FileNotFoundException;

/**
 * Created by Tao on 2016/3/22.
 */
public class PicOperator {

    public static Bitmap toRoundBitmap(Bitmap bitmap) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        int r = 0;
        if (width > height) {
            r = height;
        } else {
            r = width;
        }

        Bitmap backgroundBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(backgroundBitmap);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        RectF rectF = new RectF(0, 0, r, r);
        canvas.drawRoundRect(rectF, r / 2, r / 2, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, null, rectF, paint);
        return backgroundBitmap;
    }

    public static Bitmap decodeUriAsBitmap(Context context, Uri uri) {
        if (uri == null) {
            return null;
        }
        Bitmap bitmap = null;
        try {
            bitmap = BitmapFactory.decodeStream(context.getContentResolver().openInputStream(uri));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return bitmap;
    }

    public static void toCropImageActivity(Activity context, Uri fromFileUri, Uri saveFileUri, int outputX, int outputY, int requestCode) {
        toCropImageActivity(context, fromFileUri, saveFileUri, outputX, outputY, true, requestCode);
    }

    public static void toCropImageActivity(Activity context, Uri fromFileUri, Uri saveFileUri, int outputX, int outputY, boolean canScale, int requestCode) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(fromFileUri, "image/*");
        intent.putExtra("crop", "true");
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        intent.putExtra("outputX", outputX);
        intent.putExtra("outputY", outputY);
        intent.putExtra("scale", canScale);

        intent.putExtra("scaleUpIfNeeded", true);
        intent.putExtra("return-data", false);

        intent.putExtra(MediaStore.EXTRA_OUTPUT, saveFileUri);
        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());

        intent.putExtra("noFaceDetection", true);

        Log.d(MyApplication.TAG, "" + fromFileUri);
        Log.d(MyApplication.TAG, "" + saveFileUri);
        Log.d(MyApplication.TAG, "toCropImageActivity startActivityForResult");
        context.startActivityForResult(intent, requestCode);
    }

}
